"""
数据分析
- 以标签为组
"""

import os
import pandas as pd
from tensorboardX import SummaryWriter

tensorboard_path = "./board/"
path = "../../Data/furnace86/excel/"
# path = "../../Data/furnace86/excel/minutes/"
rise_file = "20211203-20211207.xlsx"
fall_file = "20211218-20211222.xlsx"

# 标签类别
label_list = ['pid', 'set_temp', 'temp', 'in_temp', 'mean_var', 'in_mean_var']
# 按标签数据分别写入到tensorboard
writer = [SummaryWriter(tensorboard_path+label_list[i]) for i in range(len(label_list))]


def writer_data(w, x, y, s):
    # for i in range(len(w)):
    #     w[i].add_scalar(s, y, x)
    w.add_scalar(s, y, x)


def get_xlsx_data(path='../', file='1.xlsx'):
    filepath = path + file
    df = pd.DataFrame(pd.read_excel(filepath)).fillna(0)  # 空数据补零
    # 读取外围温度和中心温度
    df = df.iloc[:, 1:]
    # df = df.fillna(df.mean())  # 填充
    # 外部温度的均方差
    df_mean = df.iloc[:, 12:18].mean(1)
    df_var = df.iloc[:, 12:18].var(1)
    df.insert(loc=24, column='mean', value=df_mean)
    df.insert(loc=25, column='var', value=df_var)
    # 中心温度的均方差
    df_mean = df.iloc[:, 18:24].mean(1)
    df_var = df.iloc[:, 18:24].var(1)
    df.insert(loc=26, column='in_mean', value=df_mean)
    df.insert(loc=27, column='in_var', value=df_var)
    return df


df_rise = get_xlsx_data(path, rise_file)
df_fall = get_xlsx_data(path, fall_file)
df_rf = pd.concat([df_rise, df_fall], axis=0, ignore_index=True)

# print(df_rf.info())
# print(df_rf.head(5))
# print(df_rf.size)
# print(df_rf.shape)


for i, row in df_rf.iterrows():
    # print(i, row['UNIT1_IR_TEMPLE1'], row['UNIT1_IR_TEMPLE2'])

    # label_list = ['pid', 'set_temp', 'temp', 'in_temp', 'mean_var', 'in_mean_var']
    for j in range(6):
        writer_data(writer[0], i, row['UNIT1_IR_PID_' + str(j + 1)], 'temp_data/pid' + str(j + 1))
    for j in range(6):
        writer_data(writer[1], i, row['UNIT1_IR_SET_TEMP_' + str(j + 1)], 'temp_data/temp' + str(j + 1))
    for j in range(6):
        writer_data(writer[2], i, row['UNIT1_IR_TEMPLE' + str(j + 1)], 'temp_data/temp' + str(j + 1))
    for j in range(6):
        writer_data(writer[3], i, row['UNIT1_IR_TEMPLETC' + str(j + 1)], 'temp_data/temp' + str(j + 1))
    writer_data(writer[4], i, row['mean'], 'unit/mean')
    writer_data(writer[4], i, row['var'], 'unit/var')
    writer_data(writer[5], i, row['in_mean'], 'unit/mean')
    writer_data(writer[5], i, row['in_var'], 'unit/var')
